﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Globalization;
using System.Text;
using System.Windows.Data;
using Xceed.Silverlight.Data.Stats;

namespace Xceed.Silverlight.DataGrid.Converters
{
  public class StatResultConverter : StringFormatConverter
  {
    #region OverflowMessage Property

    public string OverflowMessage
    {
      get
      {
        return m_overflowMessage;
      }
      set
      {
        m_overflowMessage = value;
      }
    }

    private string m_overflowMessage = "#OVER#";

    #endregion

    #region DivisionByZeroMessage Property

    public string DivisionByZeroMessage
    {
      get
      {
        return m_divisionByZeroMessage;
      }
      set
      {
        m_divisionByZeroMessage = value;
      }
    }

    private string m_divisionByZeroMessage = "#DIV/0#";

    #endregion

    #region InvalidValueMessage Property

    public string InvalidValueMessage
    {
      get
      {
        return m_invalidValueMessage;
      }
      set
      {
        m_invalidValueMessage = value;
      }
    }

    private string m_invalidValueMessage = "#VALUE#";

    #endregion

    #region NotSupportedStatFunctionMessage Property

    public string NotSupportedStatFunctionMessage
    {
      get
      {
        return m_notSupportedStatFunctionMessage;
      }
      set
      {
        m_notSupportedStatFunctionMessage = value;
      }
    }

    private string m_notSupportedStatFunctionMessage = "#NOTSUPPORTED#";

    #endregion

    #region IValueConverter Members

    public override object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
      if( ( value == null ) || ( value is DBNull ) )
        return null;

      if( value is DivideByZeroException )
        return m_divisionByZeroMessage;

      if( value is OverflowException )
        return m_overflowMessage;

      if( value is InvalidValueException )
        return m_invalidValueMessage;

      if( value is NotSupportedStatFunctionException )
        return m_notSupportedStatFunctionMessage;

      InvalidSourcePropertyNameException invalidSourcePropertyNameException = value as InvalidSourcePropertyNameException;

      if( invalidSourcePropertyNameException != null )
        return "#" + invalidSourcePropertyNameException.SourcePropertyName + "#";

      Exception exception = value as Exception;

      if( exception != null )
        return exception.Message;

      return base.Convert( value, targetType, parameter, culture );
    }

    #endregion
  }
}
